package com.douqu.game.core.entity;


import com.douqu.game.core.entity.common.CommonData;
import com.douqu.game.core.entity.sprite.Player;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.LoadUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* @author wangzhenfei
*         2017-08-08 16:06
*        副本地图
*/
public class InstanceMap extends GameObject {

    /**
     * 副本关卡
     */
    private List<Level> finishLevels;

    private Map<Integer, Level> levelMap;

    /**
     * 已经获取的地图奖励
     * Integer 记录所领取的ID
     */
    private List<Integer> hasReward;

    /**
     * 关卡列表
     */
    public int[] levels;
    /**
     * 背景
     */
    public String bg;
    /**
     * 星级宝箱
     * value: 星星数
     * id: 掉落组ID
     */
    public InstanceStarBox[] starBox;
    /**
     * 解锁副本
     */
    public int unLockInstance;

    public InstanceMap() {
        finishLevels = new CopyOnWriteArrayList<Level>();
        levelMap =  new ConcurrentHashMap();
        hasReward = new CopyOnWriteArrayList<>();
    }



    public InstanceStarBox getInstanceStarBox(int id)
    {
        for(InstanceStarBox box : starBox)
        {
            if(box.id == id)
                return box;
        }
        return null;
    }






    public Map<Integer, Level> getLevelMap() {
        return levelMap;
    }

    public void setLevelMap(Map<Integer, Level> levelMap) {
        this.levelMap = levelMap;
    }

    public List<Level> getFinishLevels() {
        return finishLevels;
    }

    public void addLevel(Level level){
        if(level != null && !levelMap.containsKey(level.getId())){
            levelMap.put(level.getId(), level);
            finishLevels.add(level);
        }

    }

    public void setFinishLevels(List<Level> finishLevels) {
        this.finishLevels = finishLevels;
    }

    public List<Integer> getHasReward() {
        return hasReward;
    }

    public void setHasReward(List<Integer> hasReward) {
        this.hasReward = hasReward;
    }



    @Override
    public void setVariable(String key, String value)
    {
        if("starBox".equals(key))
        {
            String[] strs = value.split(ConstantFactory.DIVISION);
            starBox = new InstanceStarBox[strs.length];
            for(int i = 0; i < strs.length; i++)
            {
                //配置文件里配置的是星星数在前，所以这里要先取第二个为ID
                starBox[i] = new InstanceStarBox(strs[i]);
                DropGroup dropGroup = DataFactory.getInstance().getGameObject(DataFactory.DROP_GROUP_KEY, starBox[i].dropId);
                if(dropGroup == null)
                {
                    System.out.println("InstanceMap setVariable starBox error -> DropGroup is null:" + strs[i]);
                }
                if(starBox[i].star <= 0)
                {
                    System.out.println("InstanceMap setVariable starBox count error -> " + strs[i]);
                }
            }
        }
        else if("levels".equals(key))
        {
            levels = LoadUtils.loadIntArray(key, value);
            Level level = null;
            for(int levelId : levels)
            {
                level = DataFactory.getInstance().getGameObject(DataFactory.LEVEL_KEY, levelId);
                if(level == null)
                {
                    System.out.println("InstanceMap setVariable levels error -> Level is null:" + levelId);
                }
                else
                {
                    level.setInstanceId(this.id);
                }
            }
        }
        else
            super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "InstanceMap{" +
                "id = " + id +
                ",finishLevels=" + finishLevels +
                ", hasReward=" + hasReward +
                '}';
    }
}
